package org.idea.servlet;

import org.idea.dao.UserInfoDao;
import org.idea.dao.impl.UserInfoDaoImpl;
import org.idea.entity.UserInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author zqx
 * @Date 2023-10-12
 */
@WebServlet("/reg.do")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置编码 - 防止获取表单数据乱码问题
        req.setCharacterEncoding("UTF-8");
        // 第一：获取表单输入的数据
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        String email = req.getParameter("email");

        // 第二：加工处理 -> 业务对象 -> DAO对象
        // 1：实例化 dao 对象
        UserInfoDao userInfoDao = new UserInfoDaoImpl();
        // 2：实例化用户对象，并初始化
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername(username);
        userInfo.setPassword(password);
        userInfo.setAddress(address);
        userInfo.setEmail(email);
        userInfo.setPhone(phone);

        // 3：执行数据库操作 - dao.insert
        int r = userInfoDao.insertUserInfo(userInfo) ;

        // 第三：响应
        if(r == 1) {
            resp.sendRedirect("reg-suc.jsp");
        } else {
            resp.sendRedirect("reg-err.jsp");
        }
    }
}
